const cloud = require('wx-server-sdk')

// 初始化云开发
cloud.init({
  env: 'liucheng-2gmqvcgs50e86304' // 使用指定的云环境
});

const db = cloud.database()

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const { OPENID } = wxContext
  // const { openid } = event.userInfo
  const { name, company, contact, position } = event

  console.info(event.userInfo)

  try {
    const result = await db.collection('User').add({
      data: {
        openid: OPENID,
        name,
        company,
        contact,
        position,
        createdAt: new Date()
      }
    })
    return {
      success: true,
      data: result._id
    }
  } catch (e) {
    return {
      success: false,
      error: e.message
    }
  }
}
